{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:04.926782Z",
     "start_time": "2021-05-09T20:23:04.906556Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.6.9'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from platform import python_version\n",
    "\n",
    "python_version()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# View the original blog post at [http://queirozf.com/entries/python-number-formatting-examples](http://queirozf.com/entries/python-number-formatting-examples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## round to 2 decimal places"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:06.618058Z",
     "start_time": "2021-05-09T20:23:06.606485Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8.50'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'{:.2f}'.format(8.499)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## format float as percentage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:07.510804Z",
     "start_time": "2021-05-09T20:23:07.497009Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'10.12%'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'{:.2f}%'.format(10.12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## truncate to at most 2 decimal places\n",
    "\n",
    "turn it into a string then replace everything after the second digit after the point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:08.707184Z",
     "start_time": "2021-05-09T20:23:08.702559Z"
    }
   },
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "def truncate(num,decimal_places):  \n",
    "    dp = str(decimal_places)\n",
    "    return re.sub(r'^(\\d+\\.\\d{,'+re.escape(dp)+r'})\\d*$',r'\\1',str(num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:09.830377Z",
     "start_time": "2021-05-09T20:23:09.819282Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8.49'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "truncate(8.499,decimal_places=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:10.371881Z",
     "start_time": "2021-05-09T20:23:10.359456Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8.49'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "truncate(8.49,decimal_places=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:10.839333Z",
     "start_time": "2021-05-09T20:23:10.830356Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8.4'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "truncate(8.4,decimal_places=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:11.788963Z",
     "start_time": "2021-05-09T20:23:11.779238Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "truncate(8,decimal_places=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## left padding with zeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:13.773591Z",
     "start_time": "2021-05-09T20:23:13.760535Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'00003.499'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# make the total string size AT LEAST 9 (including digits and points), fill with zeros to the left\n",
    "'{:0>9}'.format(3.499)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:14.428483Z",
     "start_time": "2021-05-09T20:23:14.419431Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'03'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# make the total string size AT LEAST 2 (all included), fill with zeros to the left\n",
    "'{:0>2}'.format(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## right padding with zeros\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:14.934223Z",
     "start_time": "2021-05-09T20:23:14.924039Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.499000000'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# make the total string size AT LEAST 11 (including digits and points), fill with zeros to the RIGHT\n",
    "'{:<011}'.format(3.499)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## comma separators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T20:23:15.342382Z",
     "start_time": "2021-05-09T20:23:15.337679Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'100,000'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"{:,}\".format(100000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
